//
//  WBMainViewController.swift
//  WSWeibo
//
//  Created by guojianfeng on 2017/7/13.
//  Copyright © 2017年 guojianfeng. All rights reserved.
//

import UIKit

class WBMainViewController: UITabBarController {
    
    let composeButton: UIButton = UIButton.yw_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpChildControllers()
        setupComposeButton()
    }
    //横竖屏控制landscape：横屏，风景  portrait：竖屏，肖像
    //专场，modal控制视屏播放
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        return .portrait
    }
    
    @objc fileprivate func composeStatus() {
        print("撰写")
        let  vc = UIViewController()
        vc.view.backgroundColor = UIColor.yw_random()
        let nav = UINavigationController(rootViewController: vc)
        present(nav, animated: true, completion: nil)
    }
}

extension WBMainViewController{
    fileprivate func setupComposeButton(){
        tabBar.addSubview(composeButton)
        let w = tabBar.bounds.width/CGFloat(childViewControllers.count) - 1
        composeButton.frame = tabBar.bounds.insetBy(dx: w * 2, dy: 0)
        composeButton.addTarget(self, action: #selector(composeStatus), for: .touchUpInside)
    }
    
    fileprivate func setUpChildControllers(){
                let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let jsonPath = (docDir as NSString).appendingPathComponent("main.json")
        var fileData = NSData(contentsOfFile: jsonPath)
        if fileData == nil {
            let path  = Bundle.main.path(forResource: "main.json", ofType: nil)
            fileData = NSData(contentsOfFile: path!)
        }

        guard let arr = try? JSONSerialization.jsonObject(with: fileData! as Data, options: []) as? [[String: AnyObject]]
        else{
            return
        }
        
//        let arr: [[String:Any]] = [
//            ["clsName":"WBHomeViewController","title":"首页","imageName":"home"
//                ,"vistorInfo":["imageName":"","message":"哈哈首页"]
//            ],
//            ["clsName":"WBMessageViewController","title":"消息","imageName":"message_center"
//                ,"vistorInfo":["imageName":"visitordiscover_image_message","message":"哈哈消息"]
//            ],
//            ["clsName":"UIViewController","title":"","imageName":""
//                ,"vistorInfo":["imageName":"","message":"哈哈"]
//            ],
//            ["clsName":"WBDiscoverViewController","title":"发现","imageName":"discover"
//                ,"vistorInfo":["imageName":"visitordiscover_image_profile","message":"哈哈发现"]
//            ],
//            ["clsName":"WBProfileViewController","title":"我","imageName":"profile"
//                ,"vistorInfo":["imageName":"visitordiscover_image_profile","message":"哈哈我"]
//            ]
//        ]
        
//        let data = try! JSONSerialization.data(withJSONObject: arr, options: [.prettyPrinted])
//        let fileURL = NSURL.fileURL(withPath: "/Users/guojianfeng/Desktop/Demo.json")
//        (data as NSData).write(to: fileURL, atomically: true)
        var arrayM = [UIViewController]()
        for dict in arr! {
            arrayM.append(controller(dict as [String : AnyObject]))
        }
        viewControllers = arrayM
    }
    
    fileprivate func controller(_ dict: [String: AnyObject]) -> UIViewController{
        guard let clsName = dict["clsName"],
            let title = dict["title"],
            let imageName = dict["imageName"],
            let cls = NSClassFromString(Bundle.main.nameSpace + "." + (clsName as! String)) as? UIViewController.Type,
        let vistorDict = dict["vistorInfo"] as? [String : String]
            else{
                return UIViewController()
        }
        
        let vc: WBBaseViewController = cls.init() as! WBBaseViewController
        vc.title = title as? String
        vc.vistorInfoDictionary = vistorDict
        vc.tabBarItem.image = UIImage.init(named: "tabbar_" + (imageName as! String))
        vc.tabBarItem.selectedImage = UIImage.init(named: "tabbar_" + (imageName as! String) + "_selected")?.withRenderingMode(.alwaysOriginal)
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .highlighted)
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 12)], for: .normal)
        let nav = WBNavigationController(rootViewController: vc)
        return nav
    }
}
